package com.gmss.crawler.enums;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Optional;

/**
 * 人员头衔枚举
 */
public enum AcademicianEnum {

    CAS_ACADEMICIAN(0, "中国科学院-院士", 0),
    CAS_DIE_ACADEMICIAN(1, "中国科学院-已故院士", 0),
    CAS_FOREIGN_ACADEMICIAN(2, "中国科学院-外籍院士", 0),
    CAS_DIE_FOREIGN_ACADEMICIAN(3, "中国科学院-已故外籍院士", 0),
    CAE_ACADEMICIAN(4, "中国工程院-院士", 0),
    CAE_DIE_ACADEMICIAN(5, "中国工程院-已故院士", 0),
    CAE_FOREIGN_ACADEMICIAN(6, "中国工程院-外籍院士", 0),
    CAE_DIE_FOREIGN_ACADEMICIAN(7, "中国工程院-已故外籍院士", 0),
    CHANGJIANG_SCHOLAR_SPECIAL(8, "长江学者-特聘教授", 5),
    TWO_CAS_CAE(9, "两院院士", 0),
    CHANGJIANG_SCHOLAR_LECTURE(10, "长江学者-讲座教授", 3),
    CHANGJIANG_SCHOLAR_YOUTH(11, "青年长江学者-青年学者", 3),
    OUTSTANDING_YOUTH(12, "杰出青年", 4),
    RESEARCH_INTEGRITY(13, "科研诚信案件", -1),
    TWO_CAS_CAE_DIE(14, "两院院士-已故院士", 0),
    TWO_CAS_CAE_FOREIGN(15, "两院院士-外籍院士", 0),
    TWO_CAS_CAE_DIE_FOREIGN(16, "两院院士-已故外籍院士", 0),
    SCHOOL_LEADER(17, "校领导", -1)
    ;

    /**
     * 编码
     */
    private Integer code;

    private String value;
    /**
     * 头衔年限,-1：无，0：终身，1：一年，2：二年，3：三年......
     */
    private Integer titleYears;

    AcademicianEnum(Integer code, String value, Integer titleYears) {
        this.code = code;
        this.value = value;
        this.titleYears = titleYears;
    }


    /**
     * 根据code获取枚举对象
     *
     * @param code
     * @return
     */
    public static AcademicianEnum getEnum(Integer code) {
        if (null == code) {
            return null;
        }
        AcademicianEnum[] values = AcademicianEnum.values();
        if (null != values && values.length > 0) {
            Optional<AcademicianEnum> first = Arrays.stream(values).filter(enu -> code.equals(enu.getCode())).findFirst();
            if (first.isPresent()) {
                return first.get();
            }
        }
        return null;
    }

    public static AcademicianEnum getEnum(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        AcademicianEnum[] values = AcademicianEnum.values();
        if (null != values && values.length > 0) {
            Optional<AcademicianEnum> first = Arrays.stream(values).filter(enu -> value.equals(enu.getValue())).findFirst();
            if (first.isPresent()) {
                return first.get();
            }
        }
        return null;
    }

    public Integer getCode() {
        return code;
    }

    public String getValue() {
        return value;
    }

    public Integer getTitleYears() {
        return titleYears;
    }


    public static void main(String[] args) {
        System.out.println(AcademicianEnum.getEnum(5).getCode());
        System.out.println(AcademicianEnum.getEnum(5).getValue());
        System.out.println(AcademicianEnum.CAS_ACADEMICIAN.getCode());
        System.out.println(AcademicianEnum.CAS_ACADEMICIAN.getValue());
        System.out.println(AcademicianEnum.getEnum("青年长江学者-青年学者").getCode());
        System.out.println(AcademicianEnum.getEnum("青年长江学者-青年学者").getValue());
        System.out.println(AcademicianEnum.getEnum("青年长江学者-青年学者").getTitleYears());
    }
}
